﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class SampleCardTaskDAO
    {
        public SampleCardTaskDAO(){}
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SampleCardTask model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "SAMPLECARDTASK");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SAMPLECARDTASK(");
            strSql.Append("ID,SID,REAMRK,CREATEUSER,CREATETIME)");
            strSql.Append(" values (");
            strSql.Append(":ID,:SID,:REAMRK,:CREATEUSER,:CREATETIME)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":SID", OracleType.Number,4),
					new OracleParameter(":REAMRK", OracleType.VarChar,50),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SID;
            parameters[2].Value = model.REAMRK;
            parameters[3].Value = model.CREATEUSER;
            parameters[4].Value = model.CREATETIME;

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(SampleCardTask model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SAMPLECARDTASK set ");
            strSql.Append("ID=:ID,");
            strSql.Append("SID=:SID,");
            strSql.Append("REAMRK=:REAMRK,");
            strSql.Append("CREATEUSER=:CREATEUSER,");
            strSql.Append("CREATETIME=:CREATETIME");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":SID", OracleType.Number,4),
					new OracleParameter(":REAMRK", OracleType.VarChar,50),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SID;
            parameters[2].Value = model.REAMRK;
            parameters[3].Value = model.CREATEUSER;
            parameters[4].Value = model.CREATETIME;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SAMPLECARDTASK ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SampleCardTask GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,SID,REAMRK,CREATEUSER,CREATETIME from SAMPLECARDTASK ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            SampleCardTask model = new SampleCardTask();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SID"].ToString() != "")
                {
                    model.SID = int.Parse(ds.Tables[0].Rows[0]["SID"].ToString());
                }
                model.REAMRK = ds.Tables[0].Rows[0]["REAMRK"].ToString();
                model.CREATEUSER = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select rownum,t.* from( s.ID,SID,s.REAMRK,s.CREATEUSER,s.CREATETIME,t.testitemname  FROM SAMPLECARDTASK s left join testdepartment t on s.sid=t.id ");
            //strSql.Append(" FROM SAMPLECARDTASK ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by  s.CREATETIME)t");
            return DbHelperOra.Query(strSql.ToString());
        }
    }
}
